# -*- coding:utf-8 -*-
# created on 2017/4/30
# 

from mathsolver.functions.base import *
from mathsolver.functions.base.base import new_latex
from mathsolver.functions.xiangliang.Vector_SanJiaoXing.Vector_SanJiao_Axis_Update import (XLBulidRegularTriangleAxis)
from mathsolver.functions.xiangliang.Vector_SanJiaoXing.base import (xl_sanjiao_area_known_update,
                                                                     XLSanJiaoXingAxisConditionEqTransform)
from mathsolver.functions.mathematica.mathematicaSolve import MathematicaSolve
from mathsolver.functions.xiangliang.basic import xl_solutions_subs
from sympy import Abs

# 在等边△ABC中,\\overrightarrow{AC}=2\\overrightarrow{AD},△ABC的面积为6\\sqrt{6}.
# 若\\overrightarrow{AP}=\\frac{1}{2}\\overrightarrow{AC}+\\frac{5}{6}\\overrightarrow{AB},则△ABP的面积为
"""
1.正三角形类
"""


class XLSanJiaoGetArea001(BaseFunction):
    """
    在等边△ABC中,\\overrightarrow{AC}=2\\overrightarrow{AD},△ABC的面积为6\\sqrt{6}.
    若\\overrightarrow{AP}=\\frac{1}{2}\\overrightarrow{AC}+\\frac{5}{6}\\overrightarrow{AB},则△ABP的面积为
    """
    def solver(self, *args):
        assert len(args) == 2
        p1, p2, p3 = args[0].value
        name = p1 + p2 + p3
        assert name in self.known
        san_jiao_xing = self.search(name)
        assert san_jiao_xing
        san_jiao_xing_type = san_jiao_xing.type
        assert san_jiao_xing_type == "vRegularTriangle"
        new_known = dict(self.known)
        stepsolver1 = XLBulidRegularTriangleAxis(new_known).solver(name)
        self.steps += stepsolver1.steps
        self.label.update(stepsolver1.label)
        san_jiao_xing = stepsolver1.output[0]
        if san_jiao_xing.point_Gravity_name:
            san_jiao_xing.get_gravity_axis()
        v_eqs = san_jiao_xing.Eqs
        target_triangle_points = args[1].value
        new_known = xl_sanjiao_area_known_update(new_known, v_eqs, san_jiao_xing, target_triangle_points)
        condition_veqs = []
        for eq in v_eqs:
            if len(eq) == 2:
                new_eqs = XLSanJiaoXingAxisConditionEqTransform(new_known).solver(BaseEq(eq)).output[0].sympify()
            else:
                new_eqs = XLSanJiaoXingAxisConditionEqTransform(new_known).solver(BaseIneq(eq)).output[0].sympify()
            condition_veqs.extend(new_eqs)
        self.steps.append(["", "依题意，得"])
        if len(condition_veqs) == 1:
            if len(condition_veqs[0]) == 2:
                self.steps.append(["", "%s" % BaseEq(condition_veqs[0]).printing()])
            else:
                self.steps.append(["", "%s" % BaseIneq(condition_veqs[0]).printing()])
        else:
            self.steps.append(["", "%s" % BaseIneqs(condition_veqs).printing()])
        solutions = MathematicaSolve().solver(BaseIneqs(condition_veqs)).output[0]
        self.steps.append(["", "解得: %s" % (solutions.printing())])
        p1, p2, p3 = args[1].value
        x1, y1 = new_known[p1].sympify()
        x2, y2 = new_known[p2].sympify()
        x3, y3 = new_known[p3].sympify()
        target_area = Abs(((x3 - x1) * (y2 - y1) - (x2 - x1) * (y3 - y1)) / 2)
        new_target_area = xl_solutions_subs(solutions, target_area)
        target_name = p1 + p2 + p3
        self.steps.append(["", "∴ △%s的面积是%s" % (new_latex(target_name), new_latex(new_target_area[0]))])
        self.output.append(BaseNumbers(new_target_area))
        self.label.add("求三角形面积-坐标法")
        return self


class XLSanJiaoGetArea(BaseFunction):
    CLS = [XLSanJiaoGetArea001]

    def solver(self, *args):
        known = self.known
        r = None
        for cl in XLSanJiaoGetArea.CLS:
            try:
                r = cl(known, verbose=True).solver(*args)
                break
            except Exception:
                pass
        if not r:
            raise 'try fail'
        return r
